using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Model;


namespace IGlobalist.Cms.Admin.Control
{
    public partial class CmsExtPropertiesForm : AdminBaseUserControl
    {
             
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            //if (ViewState["PropertyList"] != null)
            //{
            //    ExtPropertiesParser(ViewState["PropertyList"] as PropertyList);
            //}
        }

        private void RemoveMainProperties(PropertyList list)
        {
            int n=0;
            while (n<list.Count)
            {
                IContentProperty p = list[n];
                if (p.PropertyType == PropertyType.Main)
                {
                    list.Remove(p);
                }
                else
                {
                    n++;
                }
            }
        }
        public PropertyList PropertyList
        {
            get
            {
                return GetValues();
            }
            set
            {
                ViewState["PropertyList"] = value;
                if (value!=null)
                    ExtPropertiesParser(value);
            }
        }


        private PropertyList GetValues()
        {
            this.EnsureChildControls();
            PropertyList list=(PropertyList) ViewState["PropertyList"];
            if (list == null) return new PropertyList();
            foreach (BaseProperty property in list)
            {
                try
                {
                    if (property.PropertyType == PropertyType.Extention)
                    {
                        property.Value = ((CmsPropertyControl)this.FindControl(property.Name)).Property.Value;
                    }
                }
                catch
                {
                }
            }

            return list;

        }
        
        private void ExtPropertiesParser(PropertyList properties)
        {
            Table tb = new Table();
            tb.ID = "tbExtProperties";
            foreach (IContentProperty property in properties)
            {
                if (property.PropertyType == PropertyType.Extention)
                {
                    tb.Rows.Add(PropertyParser(property));
                }
            }

            this.Controls.Add(tb);
        }

        private TableRow PropertyParser(IContentProperty property)
        {
            TableRow tr = new TableRow();
            TableCell label = new TableCell();
            label.Text = property.DisplayName;
            tr.Cells.Add(label);
            TableCell cellControl = new TableCell();
            CmsPropertyControl control = new CmsPropertyControl((BaseProperty)property);
        
            cellControl.Controls.Add(control);
            tr.Cells.Add(cellControl);

            return tr;
        }

    }
}